package com.ruoyi.project.user.people.service.impl;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.project.user.people.domain.TCouple;
import com.ruoyi.project.user.people.mapper.TCoupleMapper;
import com.ruoyi.project.user.people.service.TCoupleService;
import org.hibernate.validator.constraints.br.TituloEleitoral;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class TCoupleServiceImpl implements TCoupleService {
    @Autowired
    private TCoupleMapper tCoupleMapper;
    @Autowired
    private IUserService iUserService;

    @Value("${my.mysql.post.peopleId}")
    private Long peopleId;

    @Override
    public List<TCouple> findMore(TCouple tCouple) {
        return tCoupleMapper.selectMore(tCouple);
    }

    @Override
    public Long checkedPhone(String couplePhone) {
        return tCoupleMapper.checkedPhone(couplePhone);
    }

    @Override
    public Long checkedEmail(String coupleEmail) {
        return tCoupleMapper.checkedEmail(coupleEmail);
    }

    @Transactional
    @Override
    public int add(TCouple tCouple,String couplePassword) {
        Date date = new Date();
        User user = new User();
        user.setPassword(couplePassword);
        user.setEmail(tCouple.getCoupleEmail());
        user.setPhonenumber(tCouple.getCouplePhone());
        user.setLoginName(tCouple.getCouplePhone());
        user.setUserName(tCouple.getCoupleGroomname());
        user.setCreateTime(date);
        Long[] postIds = {peopleId};
        user.setPostIds(postIds);
        int index1 = iUserService.insertUser(user);
        if (index1>0){
            tCouple.setCoupleUserId(user.getUserId());
            tCouple.setCoupleRegdate(date);
            int index2 = tCoupleMapper.add(tCouple);
            if (index2>0){
                return 1;
            }
        }
        return 0;
    }

    @Override
    public TCouple showById(Long coupleId) {
        return tCoupleMapper.showById(coupleId);
    }

    @Transactional
    @Override
    public int updatePeopleById(TCouple tCouple) {
        int index = tCoupleMapper.updateTCoupleById(tCouple);
        if (index==1){
            TCouple tCouple1 = tCoupleMapper.showById(tCouple.getCoupleId());
            User user = new User();
            user.setUserId(tCouple1.getCoupleUserId());
            user.setPhonenumber(tCouple.getCouplePhone());
            user.setUserName(tCouple.getCoupleGroomname());
            user.setLoginName(tCouple.getCouplePhone());
            user.setEmail(tCouple.getCoupleEmail());
            int index2 = iUserService.updateUser(user);
            if (index2==1){
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int deletePeople(Long[] ids) {
        int index = 0;
        for (Long id:ids) {
            TCouple tCouple = tCoupleMapper.showById(id);
            index += iUserService.deleteUserById(tCouple.getCoupleUserId());
        }
        if (index == ids.length){
            int index2 = tCoupleMapper.deletePeople(ids);
            return index2;
        }
        return 0;
    }

    @Transactional
    @Override
    public int updateState(TCouple tCouple) {
        TCouple tCouple1 = tCoupleMapper.showById(tCouple.getCoupleId());
        User user = new User();
        user.setUserId(tCouple1.getCoupleUserId());
        user.setStatus(tCouple.getCoupleState()==1?"1":"0");
        user.setUpdateBy(ShiroUtils.getLoginName());
        iUserService.updateUserInfo(user);
        return tCoupleMapper.updateState(tCouple);
    }


}
